//index.js
//获取应用实例
var app = getApp();
var routes = require('routes');  
var _openid = app.getOpenId();
var WxParse = require('../../wxParse/wxParse.js');

Page({
  data: {
    send: false,
    alreadySend: false,
    second: 60,
    disabled: true,
    buttonType: 'default',
    phoneNum: '',
    code: '',
    otherInfo: '',
    carNumber:'',
    areaName:'',
    parkingNumber:'',
    parkingDesc:'',
    randomStr:'',
    cellHeight: '120px',
    pageItems: []  
  },


  onShow: function () {
    var that = this
    console.log(app);
    //调用应用实例的方法获取全局数据      
      var pageItems = [];
      var row = [];
      var len = routes.PageItems.length;//重组PageItems  
      len = Math.floor((len + 2) / 3) * 3;
      for (var i = 0; i < len; i++) {
        if ((i + 1) % 3 == 0) {
          row.push(routes.PageItems[i]);
          pageItems.push(row);
          row = [];
          continue;
        }
        else {
          row.push(routes.PageItems[i]);
        }
      }
      wx.getSystemInfo({
        success: function (res) {
          var windowWidth = res.windowWidth;
          that.setData({
            cellHeight: (windowWidth / 3) + 'px'
          })
          console.log(that.data.cellHeight);
        },
        complete: function () {
          that.setData({
            pageItems: pageItems
          })
          console.log('九宫格的数据:' + pageItems);
        }
      })
  },



  // 手机号部分
  inputPhoneNum: function (e) {
    let phoneNum = e.detail.value
    if (phoneNum.length === 11) {
      let checkedNum = this.checkPhoneNum(phoneNum)
      if (checkedNum) {
        this.setData({
          phoneNum: phoneNum
        })
        console.log('phoneNum' + this.data.phoneNum);
        this.showSendMsg();
        this.activeButton();
      }
    } else {
      this.setData({
        phoneNum: ''
      })
      this.hideSendMsg()
    }
  },

  checkPhoneNum: function (phoneNum) {
    let str = /^1\d{10}$/
    if (str.test(phoneNum)) {
      return true
    } else {
      wx.showToast({
        title: '手机号不正确',
        image: '../../images/fail.png'
      })
      return false
    }
  },
  // 按钮
  activeButton: function () {
    let { phoneNum, code, otherInfo } = this.data
    console.log(code)
    if (phoneNum && code && otherInfo) {
      this.setData({
        disabled: false,
        buttonType: 'primary'
      })
    } else {
      this.setData({
        disabled: true,
        buttonType: 'default'
      })
    }
  },

  showSendMsg: function () {
    if (!this.data.alreadySend) {
      this.setData({
        send: true
      })
    }
  },

  hideSendMsg: function () {
    this.setData({
      send: false,
      disabled: true,
      buttonType: 'default'
    })
  },

  sendMsg: function () {
    var that = this;
    console.log("手机号:" + that.data.phoneNum);
    var openid = wx.getStorageSync('wxOpenid');
    var randomStr = that.createNonceStr();
    that.setData({
      randomStr:randomStr
    });

    wx.request({
      url: app.globalData.domain + app.globalData.subDomain + '/sms/sendSms',
      data: {
        'openid': openid,
        'phoneNumber': that.data.phoneNum,
        'code': that.data.randomStr
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },

      success: function (res) {
        console.log('短信验证码:' + res);
      }
    })
    this.setData({
      alreadySend: true,
      send: false
    })
    this.timer()
  },

  timer: function () {
    let promise = new Promise((resolve, reject) => {
      let setTimer = setInterval(
        () => {
          this.setData({
            second: this.data.second - 1
          })
          if (this.data.second <= 0) {
            this.setData({
              second: 60,
              alreadySend: false,
              send: true
            })
            resolve(setTimer)
          }
        }
        , 1000)
    })
    promise.then((setTimer) => {
      clearInterval(setTimer)
    })
  },

// 比较两个内容是否相等  
isContentEqual:function (content_1, content_2) {
    if (content_1 === content_2) {
      return true
    }
    return false
},

createNonceStr: function () {
  var code = Math.floor(Math.random() * 903000) + 1000;
  return code;
},


  onSubmit: function () {
    var that = this;
    console.log();
    var isEqual = that.isContentEqual(parseInt(that.data.code), that.data.randomStr);
    if(!isEqual){
      wx.showToast({
        title: '验证码有误',
        icon: 'success',
        duration: 2000
      });
      console.log("审核车位时需要提交的参数:" +
        that.data.phoneNum,
        that.data.carNumber,
        that.data.areaName,
        that.data.parkingNumber,
        that.data.parkingDesc
      );
    }else{
    wx.request({
      url: app.globalData.domain + app.globalData.subDomain + '/my/applySelfParking',
      data: {
        'openid': _openid,
        'user.tel': that.data.phoneNum,
        'user.carNumber': that.data.carNumber,
        'user.areaName': that.data.areaName,
        'user.parkingNumber': that.data.parkingNumber,
        'user.parkingDesc': that.data.parkingDesc
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log('提交成功:' + res.data.message);
        if (res.data.code == 100) {
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 2000
          })
        } else {
          var tip = "操作失败" + res.data.message;
          wx.showModal({
            title: '提示',
            content: tip,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }
      }
      })
    }
  },

  addCode:function(e){
    this.setData({
      code: e.detail.value
    })
  },

  tapCarNumber:function(e){
    this.setData({
      carNumber: e.detail.value
    })
  },

  
  tapAreaName:function(e) {
    this.setData({
      areaName: e.detail.value
    })
  },

  tapParkingNumber:function(e){
    this.setData({
      parkingNumber: e.detail.value
    })
  },

tapParkingDesc:function (e) {
    this.setData({
      parkingDesc: e.detail.value
    })
  }

})